package cc.cc4414.cc02.chat.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.cc4414.cc02.chat.entity.ChatGroup;
import cc.cc4414.cc02.chat.entity.GroupUser;
import cc.cc4414.cc02.chat.repository.GroupRepository;
import cc.cc4414.cc02.chat.repository.GroupUserRepository;
import cc.cc4414.cc02.chat.service.GroupService;

/**
 * @author cc 2018年9月7日
 */
@Service
public class GroupServiceImpl implements GroupService {

	@Autowired
	private GroupRepository groupRepository;

	@Autowired
	private GroupUserRepository groupUserRepository;

	@Override
	public void save(String groupName, Long userId, String nickname) {
		// 创建群组
		ChatGroup group = new ChatGroup();
		group.setCreateTime(new Date());
		group.setGroupName(groupName);
		group.setGroupOwnerId(userId);
		group.setGroupOwnerNickname(nickname);
		group.setState(0);
		ChatGroup group2 = groupRepository.save(group);
		// 自己加入群组
		GroupUser groupUser = new GroupUser();
		groupUser.setCreateTime(new Date());
		groupUser.setGroupId(group2.getId());
		groupUser.setGroupName(groupName);
		groupUser.setUserId(userId);
		groupUser.setUserNickname(nickname);
		groupUser.setLastMessageSendId(0L);
		groupUserRepository.save(groupUser);
	}

}
